﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AADD;
using Tao.OpenGl;

namespace MapEditor
{
    class Tile {
        public int prop1, prop2, prop3, prop4, prop5, prop6, prop7; // user-defined properties
        public int collisionMask;                                   // Just OR this with the directions
        public int graphicIndex;                                    // -1 if the tile isn't showing (used for layer 1 and 2)

        public Tile(int graphicIndex)
        {
            this.graphicIndex = graphicIndex;
        }
    }
    class Map
    {
        List<List<Tile>> layer0; // inner list is a row of tiles, outer list is the list of rows
        List<List<Tile>> layer1;
        List<List<Tile>> layer2;

        public int width, height; // in tiles
        public string fileName; // the loaded tilesheet

        public Map( string fileName ) : this(fileName, 100, 100)
        {
        }

        public Map(string fileName, int width, int height)
        {
            this.fileName = fileName;
            this.width = width;
            this.height = height;
            layer0 = new List<List<Tile>>();
            layer1 = new List<List<Tile>>();
            layer2 = new List<List<Tile>>();
            int c = 0;
            for (int h = 0; h < height; h++)
            {
                List<Tile> row = new List<Tile>(width);
                for (int w = 0; w < width; w++)
                {
                    Tile t = new Tile(w + c * width);
                    row.Add(t);
                }
                c++;
                layer0.Add(row);
            }
            for (int h = 0; h < height; h++)
            {
                List<Tile> row = new List<Tile>(width);
                for (int w = 0; w < width; w++)
                {
                    Tile t = new Tile(-1);
                    row.Add(t);
                }
                layer1.Add(row);
                layer2.Add(row);
            }
        }

        public void blit(ref SpritesheetManager spriteManager, int layerNumber, int x, int y, int width, int height)
        {
            // This is a reference to whatever layer we're blitting from
            List<List<Tile>> layerToUse;
            switch (layerNumber)
            {
                case 0:
                    layerToUse = layer0;
                    break;
                case 1:
                    layerToUse = layer1;
                    break;
                case 2:
                    layerToUse = layer2;
                    break;
                default:
                    throw new Exception(string.Format("Layer number {0} does not exist. (error coming from Map::blit)", layerNumber));
            }

            if (y + height > this.height)
            {
                height = this.height - y;
            }

            if (x + width > this.width)
            {
                width = this.width - x;
            }
            Gl.glPushMatrix();
            for (int row = y; row < y + height; row++)
            {
                for (int col = x; col < x + width; col++)
                {
                    int index = layerToUse[row][col].graphicIndex;

                    // don't blit if it is a blank tile, but still translate
                    if (index != -1)
                    {
                        spriteManager.blit(fileName, index);
                    }
                    Gl.glTranslatef(main.TILESIZE,0,0);
                }
                Gl.glTranslatef(-main.TILESIZE * width,main.TILESIZE,0);

            }
            Gl.glPopMatrix();

        }

    }
}
